package com.btex.app.treaty.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.btex.app.R;
import com.btex.app.tools.Tools;
import com.btex.app.treaty.ZhiJingZhuanAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zy zhangyi <zhangyi, 1104745049@QQ.com
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/10/15
 * @time 下午6:05
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */

public class ZhiJingZhuanDialog extends Dialog {


    @BindView(R.id.history_list_dialog)
    RecyclerView mHistoryListDialog;
    @BindView(R.id.history_list_dialog_quxiao)
    TextView mHistoryListDialogQuxiao;
    private ZhiJingZhuanAdapter mHistoryAdapter;
    private ArrayList<String> mMData;

    ZhangHu mZhangHu;

    public ZhiJingZhuanDialog(@NonNull Context context) {
        super(context, R.style.coupon_style);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_dialog);
        ButterKnife.bind(this);

        mHistoryListDialogQuxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


        initRecy();
        int viewHeight = Tools.getViewHeight(mHistoryListDialog);
        int tvviewHeight = Tools.getViewHeight(mHistoryListDialogQuxiao);


        //获取屏幕宽高
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        //屏幕宽
        int widthPixels = displayMetrics.widthPixels;
        //屏幕高
        int heightPixels = displayMetrics.heightPixels;
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        //设置给attributes
        attributes.width = (widthPixels * 1);

        if (viewHeight > 0) {
            attributes.height = viewHeight + tvviewHeight;
        } else {
            attributes.height = (int) (heightPixels * 0.6);
        }


        //让Dialog位置居中
        attributes.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(attributes);
        //点击空白处不dismiss
        setCanceledOnTouchOutside(true);


    }

    private void initRecy() {


        mHistoryAdapter = new ZhiJingZhuanAdapter();
        mHistoryListDialog.setLayoutManager(new LinearLayoutManager(getContext()));
        mHistoryListDialog.setAdapter(mHistoryAdapter);
        mHistoryAdapter.setNewData(mMData);

        if (mHistoryAdapter != null) {
            mHistoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (mZhangHu != null) {
                        String va = (String) adapter.getData().get(position);
                        mZhangHu.get(va);
                        dismiss();
                    }

                }
            });
        } else {
        }


    }


    public void setData(ArrayList<String> mData) {

        mMData = mData;
    }

    public interface ZhangHu {

        public void get(String data);
    }


    public void getValue(ZhangHu ZhangHu) {

        this.mZhangHu = ZhangHu;

    }


}
